package com.cskaoyan.service;

import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketTopicExample;
import com.cskaoyan.bean.vo.WxTopicDetailVo;
import com.cskaoyan.bean.vo.WxTopicListVo;
import com.cskaoyan.bean.vo.WxTopicRelatedVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Trellis
 * @since 2022/09/09 10:53
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public WxTopicListVo selectListByLimit(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);

        MarketTopicExample example = new MarketTopicExample();
        WxTopicListVo wxTopicListVo = new WxTopicListVo();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);
        for (MarketTopic marketTopic : marketTopics) {
            wxTopicListVo.copyList(marketTopic);
        }

        PageInfo pageInfo = new PageInfo(marketTopics);
        int total = (int) pageInfo.getTotal();
        wxTopicListVo.setPages(pageInfo.getPages());
        wxTopicListVo.setTotal(total);
        wxTopicListVo.setPage(page);
        wxTopicListVo.setLimit(limit);
        return wxTopicListVo;
    }

    @Override
    public WxTopicDetailVo selectDetailById(Integer id) {
        WxTopicDetailVo wxTopicDetailVo = new WxTopicDetailVo();
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        wxTopicDetailVo.copyTopic(marketTopic);

        Integer[] goods = marketTopic.getGoods();
        for (Integer good : goods) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(good);
            wxTopicDetailVo.copyGoods(marketGoods);
        }

        return wxTopicDetailVo;
    }

    @Override
    public WxTopicRelatedVo selectRelatedById(Integer id) {
        int page = 1;
        int limit = 10;
        PageHelper.startPage(page,limit);
        WxTopicRelatedVo wxTopicRelatedVo = new WxTopicRelatedVo();
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdBetween(id,id + 10);
        }
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketTopics);
        for (MarketTopic marketTopic : marketTopics) {
            wxTopicRelatedVo.copyList(marketTopic);
        }
        wxTopicRelatedVo.setLimit(limit);
        wxTopicRelatedVo.setPage(page);
        wxTopicRelatedVo.setPages(pageInfo.getPages());
        wxTopicRelatedVo.setTotal((int)pageInfo.getTotal());


        return wxTopicRelatedVo;
    }
}
